#include "EditorWindowViewModel.h"
#include "EditorWindowModel.h"

#include "Math/Rect.h"
#include "Widgets/Window.h"

#include "Asset/EngineDefaultPackage.h"
#include "Asset/EditorDefaultPackage.h"


#include "PageArea/EditorPageAreaManagerModel.h"
#include "PageArea/EditorPageAreaManagerViewModel.h"

#include "EditorWindowGroupModel.h"

void REditorWindowViewModel::BindModel(TSharedObjectPtr<REditorWindowModel> InModel)
{
    BaseModel=InModel;


    BaseModel->OnWindowSizeTypeChanged.Bind([this]()
    {
        OnWindowSizeTypeChanged.ExecuteIfBound();
    });

    BaseModel->OnWindowSizeChanged.Bind([this]()
    {
        OnWindowSizeChanged.ExecuteIfBound();
    });

    BaseModel->OnWindowPositionChanged.Bind([this]()
    {
        OnWindowPositionChanged.ExecuteIfBound();
    });


    MinimizeButtonImage=REditorDefaultPackage::Get()->GetMinimizeIcon(); 
    RestoreButtonImage=REditorDefaultPackage::Get()->GetRestoreIcon();
    MaximizeButtonImage=REditorDefaultPackage::Get()->GetMaximizeIcon();
    CloseButtonImage=REditorDefaultPackage::Get()->GetCloseIcon();
    EngineIcon=REngineDefaultPackage::Get()->GetEngineIcon();

    PageAreaViewModel=NewObject<REditorPageAreaManagerViewModel>();
    PageAreaViewModel->BindModel(BaseModel->GetPageArea());

}

TSharedObjectPtr<REditorWindowModel> REditorWindowViewModel::GetModel()
{
    return BaseModel;
}

void REditorWindowViewModel::OnGotUserFocus()
{
    BaseModel->Focuse();
}

CString REditorWindowViewModel::GetWindowTitle() const
{
    return WindowTitle;
}

void REditorWindowViewModel::SetWindowTitle(const CString &InTitle)
{
    WindowTitle = InTitle;
    OnWindowTitleChanged.ExecuteIfBound();
}

EWindowSizeType REditorWindowViewModel::GetWindowSizeType() const
{
    return BaseModel->GetWindowSizeType();
}

void REditorWindowViewModel::SetWindowSizeType(EWindowSizeType InType)
{
    BaseModel->SetWindowSizeType(InType);
}

SIntPoint REditorWindowViewModel::GetWindowSize() const
{
    return BaseModel->GetWindowSize();
}

void REditorWindowViewModel::SetWindowSize(const SIntPoint &InSize)
{
    BaseModel->SetWindowSize(InSize);
}

SIntPoint REditorWindowViewModel::GetWindowPosition(TSharedObjectPtr<RWindow> InWindow) const
{
    SIntPoint DesiredPosition = BaseModel->GetWindowPosition();
    if (DesiredPosition.X == std::numeric_limits<int64_t>::max() || DesiredPosition.Y == std::numeric_limits<int64_t>::max())
    {
        //put to screen center
        SRect PlacebleArea = InWindow->GetPlacebleArea();
        SIntPoint WindowSize = GetWindowSize();
        DesiredPosition.X = PlacebleArea.Min.X + (PlacebleArea.Extent.X - WindowSize.X) / 2;
        DesiredPosition.Y = PlacebleArea.Min.Y + (PlacebleArea.Extent.Y - WindowSize.Y) / 2;

    }

    return DesiredPosition;
}

void REditorWindowViewModel::SetWindowPosition(const SIntPoint &InPosition)
{
    BaseModel->SetWindowPosition(InPosition);
}

bool REditorWindowViewModel::IsResizable() const
{
    return BaseModel->GetWindowSizeType() == EWindowSizeType::Normal;
}

TSharedObjectPtr<RTexture2D> REditorWindowViewModel::GetMinimizeButtonImage() const
{
    return MinimizeButtonImage;
}

TSharedObjectPtr<RTexture2D> REditorWindowViewModel::GetMaximizeButtonImage() const
{
    return BaseModel->GetWindowSizeType() == EWindowSizeType::Maximized ? RestoreButtonImage : MaximizeButtonImage;
}

TSharedObjectPtr<RTexture2D> REditorWindowViewModel::GetCloseButtonImage() const
{
    return CloseButtonImage;
}

TSharedObjectPtr<RTexture2D> REditorWindowViewModel::GetWindowLeftTopIcon() const
{
    return EngineIcon;
}

TSharedObjectPtr<REditorPageAreaManagerViewModel> REditorWindowViewModel::GetPageArea() const
{
    return PageAreaViewModel;
}
